package com.example.java_chatroom1.mapper;

import com.example.java_chatroom1.model.Friend;
import com.example.java_chatroom1.model.MessageInfo;
import com.example.java_chatroom1.model.MessageSession;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface MessageMapper {
    @Select("select sessionId from message_session where sessionId in (select sessionId from message_session_user where userId=#{userId}) order by lastTime desc")
    List<Integer> getSessionIdByUserId(@Param("userId") Integer userId);

    @Select("select userId as friendId,username as friendName from user where userId in (select userId from message_session_user where sessionId=#{sessionId} and userId != #{selfUserId})")
    List<Friend> getFriendBySessionId(@Param("sessionId") Integer sessionId, @Param("selfUserId") int selfUserId);

    @Select("select userId from message_session_user where sessionId=#{sessionId} and userId != #{userId}")
    Integer getUserIdBySessionId(@Param("sessionId") Integer sessionId,@Param("userId") Integer userId);

    Integer addIntoMessageSession(MessageSession messageSession);

    @Insert("insert into message_session_user values (#{sessionId},#{userId})")
    Integer addIntoMessageSessionUser(@Param("sessionId") Integer sessionId,@Param("userId") Integer userId);

    @Insert("insert into message(sessionId,fromId,message,postTime) values(#{sessionId},#{fromId},#{message},now())")
    Integer addMessageInfo(MessageInfo messageInfo);
}
